var question = "";
var reponses = "";
var checkReponses = [];
var existingReponsesEventuellesForQuestion = [];
var existingReponsesCorrectesForQuestion = [];

function addReponseCorrecteToQuestion(){
	
	while (checkReponses.length > 0) {
		checkReponses.pop();
	}
	
	$("input:checked").each(function(){
		checkReponses.push($(this).val());
	});
	
	if (checkReponses.length > 0){
		jCheckReponses = JSON.stringify(checkReponses);
		
		var data = {
			idQuestion : getParameterByName("idQuestion"),
			idReponses : jCheckReponses
		};
		
		$.ajax({
			type : 'Post',
			url : '/qRH-WS/services/question/addReponsesCorrectes',
			async: false,
			dataType : 'json',
			data : JSON.stringify(data),
			crossDomain : true,
			success : function(result, textStatus, jqXHR) {
				if(result != null){
					window.location.replace("question.html");
				} else {
					alert("Une erreur est survenue lors de la sauvegarde des reponses correctes");
				}
			},
			error : function(xhr, ajaxOptions, thrownError) {
				alert(xhr.status);
				alert(xhr.responseText);
			}
		});
	} else {
		alert("Veuillez selectionner au moins une reponse correcte pour cette question");
	}
}


function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

$(document).ready(
	function(){
		
		var data = {
			idQuestion : getParameterByName("idQuestion")
		};
		$.ajax({
			type : 'Get',
			url : '/qRH-WS/services/question/getQuestionById',
			async: false,
			dataType : 'json',
			data : data,
			crossDomain : true,
			success : function(result, textStatus, jqXHR) {
				question = result;
				var parent = $('aside > article > hgroup ');
				$('<h3>').text(question.contenuQuestion).appendTo(parent);
				existingReponsesEventuellesForQuestion = eval(question.reponsesEventuelles);
				existingReponsesCorrectesForQuestion = eval(question.reponsesCorrectes);
			},
			error : function(xhr, ajaxOptions, thrownError) {
				alert(xhr.status);
				alert(xhr.responseText);
			}
		});

		var chkReponses = $('#chkReponses');

		for(var i = 0; i < existingReponsesEventuellesForQuestion.length; i++){
			put = false;
			for(var j = 0; j < existingReponsesCorrectesForQuestion.length; j++){
				if(existingReponsesEventuellesForQuestion[i].idReponse ==  existingReponsesCorrectesForQuestion[j].idReponse){
					$('<input type="checkbox" checked="checked" />').val(existingReponsesEventuellesForQuestion[i].idReponse).attr("id","chk"+existingReponsesEventuellesForQuestion[i].idReponse).appendTo(chkReponses);
					put = true;
				}
			}
			if(put === false){
				$('<input type="checkbox" />').val(existingReponsesEventuellesForQuestion[i].idReponse).attr("id","chk"+existingReponsesEventuellesForQuestion[i].idReponse).appendTo(chkReponses);
			}
			$('<label for="chk'+existingReponsesEventuellesForQuestion[i].idReponse+'" />').text(existingReponsesEventuellesForQuestion[i].contenuReponse).appendTo(chkReponses);
			$('<br/>').appendTo(chkReponses);
		}
}
);